/* 2014/8/17 */
package com.cosmos.quartz.launch;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.thoughtworks.xstream.XStream;

class LaunchConfigSerializer {

	private static final XStream xStream;
	
	static {
		xStream = new XStream();
		xStream.autodetectAnnotations(true);
		xStream.processAnnotations(LaunchConfig.class);
	}
		
	public static LaunchConfig load(String configFilePath) throws FileNotFoundException, IOException {
		LaunchConfig launchConfig = null;
		FileInputStream fileInputStream = null;
		try {
			File configFile = new File(configFilePath);
			fileInputStream = new FileInputStream(configFile);
		
			launchConfig = (LaunchConfig)xStream.fromXML(fileInputStream);
			
		} catch (Exception ex) {
			throw ex;
		} finally {
			if (fileInputStream != null) {
				fileInputStream.close();
			}			
		}		
		
		return launchConfig;
	}
	
	public static void save(LaunchConfig launchConfig, String configFilePath) throws FileNotFoundException, IOException {
		FileOutputStream fileOutputStream = null;
		try {
			File configFile = new File(configFilePath);
			fileOutputStream = new FileOutputStream(configFile);
			
			xStream.toXML(launchConfig, fileOutputStream);
		} catch (Exception ex) {
			throw ex;
		} finally {
			if (fileOutputStream != null) {
				fileOutputStream.close();
			}
		}		
	}	
}
